﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AddIn.EA.Architecture.ComponentLogic.ElementModel;

namespace AddIn.EA.Architecture.ComponentLogic.StandardProject.Builders.UI
{
    public class ElementBuilderModel : ElementBuilder
    {
        public override void BuildStructure(ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            ///Creating the Model element.
            BuildModelElement(viewModel, templateRepository, itemEntity);
            //Creating the Filter Class.
            if (itemEntity.HasFilter)
                BuildModelFilterElement(viewModel, templateRepository, itemEntity);
            //Creating the FilterKey Class.
            if (itemEntity.HasKeyFilter)
                BuildModelFilterKeyElement(viewModel, templateRepository, itemEntity);
        }
        public void BuildModelElement(ComponentLogic.ElementModel.Package viewModel, Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = GetTemplatePackage(templateRepository);
            AddIn.EA.Framework.Element templateModelElement = templatePackage.GetElementByName("[entity_name]");

            //Setup the Model package
            ComponentLogic.ElementModel.Package ModelPackage = GetModelPackage(viewModel);

            //Set Model name.
            String elementName = templateModelElement.Name.Replace("[entity_name]", itemEntity.Name);

            //Prevent existing item before include
            ModelPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementName) == 0);

            //Create the Entity Element
            Element ModelElement = BuildEntityElementFromTemplate(itemEntity, templateModelElement, elementName);

            //Create the Entity Attributes/Properties.
            BindElementProperties(ModelElement, itemEntity);

            ModelPackage.Elements.Add(ModelElement);
        }
        public void BuildModelFilterElement(ComponentLogic.ElementModel.Package viewModel, Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = GetTemplatePackage(templateRepository);
            AddIn.EA.Framework.Element templateModelParameterFilterElement = templatePackage.GetPackageByName("Parameter").GetElementByName("Filtro[entity_name]");

            ComponentLogic.ElementModel.Package parameterPackage = GetModelParameterPackage(viewModel);

            String elementFilterName = templateModelParameterFilterElement.Name.Replace("[entity_name]", itemEntity.Name);
            //Prevent existing item before include.
            parameterPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementFilterName) == 0);

            //Build the filter element.
            Element ModelFilterElement = BuildEntityElementFromTemplate(itemEntity, templateModelParameterFilterElement, elementFilterName);
            
            foreach (ComponentLogic.StandardProject.PropertyStandardProject itemProerty in itemEntity.Properties.FindAll(it => it.ComposeFilter))
                ModelFilterElement.Attributes.Add(CreateElementModelAttribute(itemProerty));

            parameterPackage.Elements.Add(ModelFilterElement);
        }
        public void BuildModelFilterKeyElement(ComponentLogic.ElementModel.Package viewModel, Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = GetTemplatePackage(templateRepository);
            AddIn.EA.Framework.Element templateModelParameterKeyElement = templatePackage.GetPackageByName("Parameter").GetElementByName("Filtro[entity_name]Chave");

            ComponentLogic.ElementModel.Package parameterPackage = GetModelParameterPackage(viewModel);

            String elementKeyName = templateModelParameterKeyElement.Name.Replace("[entity_name]", itemEntity.Name);
            //Prevent existing item before include.
            parameterPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementKeyName) == 0);

            //Build the filter element.
            Element ModelKeyElement = BuildEntityElementFromTemplate(itemEntity, templateModelParameterKeyElement, elementKeyName);
            
            foreach (ComponentLogic.StandardProject.PropertyStandardProject itemProerty in itemEntity.Properties.FindAll(it => it.ComposeKey))
                ModelKeyElement.Attributes.Add(CreateElementModelAttribute(itemProerty));

            parameterPackage.Elements.Add(ModelKeyElement);
        }
        private Framework.Package GetTemplatePackage(Framework.RepositoryManager templateRepository)
        {
            return templateRepository.GetPackageByName("UI").GetPackageByName("Model");
        }
        private Package GetModelPackage(ComponentLogic.ElementModel.Package viewModel)
        {
            ComponentLogic.ElementModel.Package uiPackage = GetViewModelPackage("UI", viewModel, true);
            ComponentLogic.ElementModel.Package modelPackage = GetViewModelPackage("Model", uiPackage, true);
            return modelPackage;
        }
        private Package GetModelParameterPackage(ComponentLogic.ElementModel.Package viewModel)
        {
            ComponentLogic.ElementModel.Package modelPackage = GetModelPackage(viewModel);
            ComponentLogic.ElementModel.Package parameterPackage = GetViewModelPackage("Parameter", modelPackage, true);
            return parameterPackage;
        }

        public override void Validate()
        {
        }
    }
}
